// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {      //这里是为了方便我做头部切换样式定义的    
    products: [
      {
          "id": 10017,
          "category": "饮料",
          "name": "咖啡",
          "description": "香浓的现磨咖啡，提神醒脑",
          "price": 20,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.d3e0603de2bfc9b811df55820c7a1a32?rik=DUR1qa4femur6w&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50073%2f8464.jpg_wh1200.jpg&ehk=El%2fvrDPlNonn%2f7EQsDx2tDZHuHQsYwfvUX5cv7ifjU0%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10016,
          "category": "饮料",
          "name": "鲜榨橙汁",
          "description": "新鲜的橙汁，维生素丰富",
          "price": 15,
          "image_url": "https://tse3-mm.cn.bing.net/th/id/OIP-C.muk6x90XSjrlHCPuAxrLXQHaIA?rs=1&pid=ImgDetMain",
          "num": 0
      },
      {
          "id": 10015,
          "category": "饮料",
          "name": "柠檬冰茶",
          "description": "清爽的柠檬冰茶，解渴佳品",
          "price": 10,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.afd3567e997963984eeb432f8b96b894?rik=Tz%2f6iE4eUmPfjg&riu=http%3a%2f%2fpic.qianye88.com%2f4kcc0c32a3083-23d4-3fdd-848b-5ce4363657bc.jpeg&ehk=ssdUrgWNPqxti3b1dlTiFj5fDFPAYVRM6%2b3jXMj6HFo%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10014,
          "category": "甜点",
          "name": "红豆沙",
          "description": "传统的红豆沙，甜而不腻",
          "price": 12,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.2bc0f68c207b3803e6a5311e73584a3e?rik=Qog6ZwS1rnvT2g&riu=http%3a%2f%2fi2.chuimg.com%2f6e1f2932890a11e6a9a10242ac110002_800w_800h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=uvSQV62SmvzzRHnBXFCag4xbP15UgylY%2buvLOpd3dMo%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10013,
          "category": "甜点",
          "name": "芒果布丁",
          "description": "香甜的芒果布丁，口感细腻",
          "price": 15,
          "image_url": "https://tse2-mm.cn.bing.net/th/id/OIP-C.ez25r2k_DXVInRe4F7kNtAHaFy?rs=1&pid=ImgDetMain",
          "num": 0
      },
      {
          "id": 10012,
          "category": "主食",
          "name": "牛肉面",
          "description": "香辣的牛肉面，配以新鲜的蔬菜",
          "price": 28,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.28ae6ae46b020f028c9e4c57dd33a6a4?rik=Yf5X8j%2fBEGb8VQ&riu=http%3a%2f%2fi2.chuimg.com%2f8651538e89ac11e6b87c0242ac110003_1280w_1280h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=fYRGtVcCd%2fJ4%2bWEmYPrL7cTEw7yX6eE4WTfBTj0Qc%2f4%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10011,
          "category": "主食",
          "name": "扬州炒饭",
          "description": "经典的扬州炒饭，内有虾仁、鸡蛋等",
          "price": 20,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.e638c892aeabcbf7e893bac560a6acac?rik=9QvszcMMSq2jFw&riu=http%3a%2f%2fi2.chuimg.com%2f3b87d54c89b811e6a9a10242ac110002_1280w_1280h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=1Mvempk5YqNFJS00Esl3MaNQaF2kng30fvrvTf%2b7kpk%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10010,
          "category": "素菜",
          "name": "香菇青菜",
          "description": "鲜香的香菇，配以翠绿的青菜",
          "price": 25,
          "image_url": "https://cp1.douguo.com/upload/caiku/5/b/8/yuan_5be454733791f1bd0139859ae9146d78.jpg",
          "num": 0
      },
      {
          "id": 10009,
          "category": "素菜",
          "name": "麻婆豆腐",
          "description": "口感丰富的麻婆豆腐，微辣",
          "price": 35,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.380e4c48aa41d8e6bbade13bec380dd2?rik=oxXEr4y9FfkZxw&riu=http%3a%2f%2fcp1.douguo.net%2fupload%2fcaiku%2f1%2fb%2f1%2fyuan_1bd4d6bc046cf44a964433ba3ff6eeb1.jpg&ehk=rEsFLZsOmT1Ky0UcNK7Owl61MyWmlVW0dvqRg4mVgwU%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10008,
          "category": "汤",
          "name": "玉米排骨汤",
          "description": "鲜甜的玉米和嫩滑的排骨炖制而成",
          "price": 30,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.1363d4c95a287dc72178d27669c4469b?rik=pgUP9ES3BYpgng&riu=http%3a%2f%2fcp1.douguo.net%2fupload%2fcaiku%2ff%2f0%2fb%2fyuan_f0a7cfafd0ed10708f924d769a0d874b.jpg&ehk=4ZZ%2bX7zqqkD8ugO7iEdluHeiI15aDr2rq89tfwJvrEw%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10007,
          "category": "汤",
          "name": "酸辣汤",
          "description": "经典的酸辣汤，配以木耳和豆腐",
          "price": 25,
          "image_url": "https://pic.nximg.cn/file/20230601/33857552_172321970108_2.jpg",
          "num": 0
      },
      {
          "id": 10006,
          "category": "主菜",
          "name": "清蒸鲈鱼",
          "description": "新鲜的鲈鱼，清蒸至完美",
          "price": 75,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.2efd857e898bb3db843c3d06301e511a?rik=6Egwb2OfxJvlvQ&riu=http%3a%2f%2fs2.cdn.xiachufang.com%2fb74c441088fa11e6b87c0242ac110003_640w_796h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=6g0evUhR6kom%2fg9hRAekpyb8eur%2fLGaH1aFxVTJg%2fws%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10005,
          "category": "主菜",
          "name": "宫保鸡丁",
          "description": "辣味十足的鸡丁，配以花生和红辣椒",
          "price": 45,
          "image_url": "https://tse2-mm.cn.bing.net/th/id/OIP-C.u07qpUgsweC4U0JLY1Ox8QAAAA?rs=1&pid=ImgDetMain",
          "num": 0
      },
      {
          "id": 10004,
          "category": "主菜",
          "name": "红烧牛肉",
          "description": "鲜嫩的牛肉块，搭配香浓的红烧酱汁",
          "price": 60,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.bc19683715060254450e6143f83512b1?rik=FUws3LuqAcFlTA&riu=http%3a%2f%2fi2.chuimg.com%2feea3d6a1749445c0999e0c7369a027e9_3024w_3024h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=XQ6YCJGXTPS7pq%2fpVWOJ%2forYIyceax9U6hEJ90J0qlI%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10003,
          "category": "前菜",
          "name": "炸春卷",
          "description": "外脆里嫩的炸春卷，内有蔬菜和猪肉馅",
          "price": 20,
          "image_url": "https://cp1.douguo.com/upload/caiku/3/c/2/yuan_3c62f5bbe5688780a819852a3d495452.jpg",
          "num": 0
      },
      {
          "id": 10002,
          "category": "前菜",
          "name": "蒜蓉烤虾",
          "description": "鲜嫩烤虾，搭配香浓蒜蓉酱",
          "price": 30,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.6eeeef6aef384f4aaf9cf7675a791639?rik=%2fKPoUWZsceyptQ&riu=http%3a%2f%2fi2.chuimg.com%2f384d40da884411e6b87c0242ac110003_640w_640h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=AEntHf%2bJBt9EKPR7AnvpDb%2b%2bg3Z5V4GRnDMsAnEbpIk%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      },
      {
          "id": 10001,
          "category": "前菜",
          "name": "凉拌黄瓜",
          "description": "清爽的凉拌黄瓜，配以蒜蓉和香油",
          "price": 15,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.214466f9bb9fea5243c8b1c0f3a80f0f?rik=KWZwRK7NsoDd%2fA&riu=http%3a%2f%2fi3.meishichina.com%2fattachment%2frecipe%2f2017%2f07%2f04%2f20170704149916366743910121381.jpg%40!p800&ehk=AiMHzI0p4CLOmDODLvxUYLN1sogJknw23%2fet9Ey9y9M%3d&risl=&pid=ImgRaw&r=0",
          "num": 0
      }
  ],

    name: '',
    totalPrice: 0,
    isHidden: true
  },

  getlist() {
    let that = this
    wx.request({
      url: "http://localhost:7000/menu-service/menuList?name=" + this.data.name,
      method: "GET",
      // success(res) {
      //   console.log(res.data);
      //   that.setData({
      //     products: res.data
      //   })
      // }
    })
  },
  //增加份量方法
  add(e) {
    console.log(e)
    var i = e.currentTarget.dataset.index;
    console.log(i);
    this.setData({
      ['products[' + i + '].num']: this.data.products[i].num + 1
    })
    this.calculateTotalPrice();
  },

  //减少份量方法
  minus(e) {
    console.log(e)
    var i = e.currentTarget.dataset.index;
    console.log(i);
    if (this.data.products[i].num > 0) {
      this.setData({
        ['products[' + i + '].num']: this.data.products[i].num - 1
      })
    }
    this.calculateTotalPrice();

  },

  xq(e) {
    console.log(e)
  },

  onLoad: function (options) {
    this.getlist()  //进入页面就要显示已经请求完了的数据，所以要把getlist（）放在onLoad里面 
    // this.calculateTotalPrice();
  },

  calculateTotalPrice: function () {
    let total = 0;
    console.log(this.data.products.length)
    for (let i = 0; i < this.data.products.length; i++) {
      total += this.data.products[i].price * this.data.products[i].num;
    }
    this.setData({
      totalPrice: total
    });
    if (this.data.totalPrice != 0) {
      this.setData({
        isHidden: false
      })
    } else {
      this.setData({
        isHidden: true
      })
    }
  },

  addShopCar: function () {
    let that = this
    for (let i = 0; i < this.data.products.length; i++) {
      if (this.data.products[i].num != 0) {
        wx.request({
          url: "http://localhost:7000/menu-service/insertShopCar",
          data: JSON.stringify(this.data.products[i]),
          method: "POST",
          header: {
            'content-type': 'application/json'
          },
          // success(res) {
          //   // 在需要刷新页面的地方调用此方法
          //   console.log(res.data)
          //   if (res.data) {
          //     wx.startPullDownRefresh();
          //   }
          // }
        })
        this.onLoad()
      }
    }
  }



})
